'use strict';
const {
	verifyToken
} = require('wx-common');
const db = uniCloud.database();
const bannerCollection = db.collection('xxf-banner');
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event);
	const {
		action,
		token,
		uid,
	} = event;
	if (!verifyToken(token)) {
		return {
			code: 401,
			message: 'Token无效'
		};
	}
	if (!uid) {
		return {
			msg: '用户ID参数错误！',
			code: 0,
			data: null
		};
	}
	switch (action) {
		case 'getBanner':
			return getBanner(event);
		default:
			return {
				msg: 'Invalid action！',
					code: 0,
					data: null
			};
	}
};

async function getBanner(params) {
	const {
		uid,
	} = params;
	const bannerRes = await bannerCollection.get();
	console.log('bannerRes: ', bannerRes);
	if (bannerRes.data && bannerRes.data.length > 0) {
		return {
			msg: '',
			code: 1,
			data: {
				list: bannerRes.data,
			}
		};
	} else {
		return {
			msg: '暂无数据！',
			code: 0,
			data: {
				total: 0,
				list: []
			}
		};
	}

}